package com.zhihui.online.controller;


import cn.hutool.core.util.ObjectUtil;
import com.zhihui.online.dto.menu.AssignMenuDTO;
import com.zhihui.online.dto.menu.MenuInsertDTO;
import com.zhihui.online.dto.menu.MenuUpdateDTO;
import com.zhihui.online.entity.Menu;
import com.zhihui.online.enums.MenuTypeEnum;
import com.zhihui.online.exception.MyCustomerException;
import com.zhihui.online.service.MenuService;
import com.zhihui.online.utils.Result;
import com.zhihui.online.utils.ResultCodeEnum;
import com.zhihui.online.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 程序员阿良, 21岁纯情后爸, 懒得想名字
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/online/menu")
@Api(tags = "菜单管理")
public class MenuController {

    @Resource
    private MenuService menuService;

    // 加载树形菜单
    @ApiOperation("菜单列表")
    @GetMapping("/findNodes")
    public Result<List<MenuVO>> findNodes() {
        List<MenuVO> menuList = this.menuService.findNodes();
        return Result.ok(menuList);
    }

    // 添加菜单
    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public Result<Object> addMenu(@RequestBody MenuInsertDTO menuInsertDTO) {
        //字段校验
        if (menuInsertDTO == null) {
            throw new MyCustomerException(ResultCodeEnum.DATA_ERROR);
        }
        // 名字校验
        if (menuInsertDTO.getName() == null) {
            throw new MyCustomerException(ResultCodeEnum.DATA_MISS);
        }
        //非目录必要参数校验
        if (!MenuTypeEnum.CATALOG.getCode().equals(menuInsertDTO.getType())) {
            //校验父级Id
            if (ObjectUtil.hasNull(menuInsertDTO.getParentId())) {
                throw new MyCustomerException(ResultCodeEnum.DATA_MISS);
            }
            //菜单，校验路由地址、组件地址
            if (MenuTypeEnum.MENU.getCode().equals(menuInsertDTO.getType())) {
                if (ObjectUtil.hasNull(menuInsertDTO.getPath(), menuInsertDTO.getComponent())) {
                    throw new MyCustomerException(ResultCodeEnum.DATA_MISS);
                }
            } else {
                //按钮必要字段校验
                // 校验权限标识
                if (ObjectUtil.hasNull(menuInsertDTO.getPerms())) {
                    throw new MyCustomerException(ResultCodeEnum.DATA_MISS);
                }
            }
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuInsertDTO, menu);
        boolean save = this.menuService.save(menu);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("/updateMenu")
    public Result<Object> updateById(@RequestBody MenuUpdateDTO menuUpdateDTO) {
        if (menuUpdateDTO.getId() == null) {
            throw new MyCustomerException(ResultCodeEnum.DATA_MISS);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuUpdateDTO, menu);
        boolean flag = this.menuService.updateById(menu);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据Id删除菜单")
    @DeleteMapping("/removeMenu/{id}")
    public Result<Object> removeMenu(@PathVariable Long id) {
        this.menuService.removeMenuById(id);
        return Result.ok();
    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result<List<MenuVO>> toAssign(@PathVariable Long roleId) {
        List<MenuVO> list = this.menuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result<Object> doAssign(@RequestBody AssignMenuDTO assignMenuDTO) {
        if (ObjectUtil.hasNull(assignMenuDTO)) {
            throw new MyCustomerException(ResultCodeEnum.DATA_MISS);
        }
        this.menuService.doAssign(assignMenuDTO);
        return Result.ok();
    }

}

